<?php

class PageIndexAction extends SzAbstractAction
{
    const SZ_PAY_COMPLETE = 'SzSdkPayComplete';
    const FB_PAY_COMPLETE = 'FacebookPayComplete';
    const PAYMENT_PRODUCT = 'product';

    /**
     * Index action entrance.
     *
     * @return SzResponse
     */
    public function execute()
    {
        $act = SzParam::getReqParam('act');
        $response = null;
        if (!$act) {
            $response = $this->displayFacebookIndex();
        } else {
            switch ($act) {
                case self::PAYMENT_PRODUCT:
                    $response = $this->displayPaymentProductInfo();
                    break;
                case self::SZ_PAY_COMPLETE:
                    $response = $this->completePaymentOrder('SzSdk');
                    break;
                case self::FB_PAY_COMPLETE:
                    $response = $this->completePaymentOrder('facebook');
                    break;
                default:
                    $response = $this->displayFacebookIndex();
            }
        }
        return $response;
    }


    private function displayFacebookIndex()
    {       
        return null;
    }

    /**
     * Payment callback for platform payment API.
     * @param string $platform
     * @return SzResponse
     */
    private function completePaymentOrder($platform)
    {
        /** @var ModulePaymentOrderVo $result */
        $result = PayService::get()->completePaymentOrder($platform);

        $response = new SzResponse();
        if ($result instanceof ModulePaymentOrderVo) {
            SzLogger::get()->debug('completePaymentOrder:' . json_encode($result->toArray()));
            $response->setBody($result->toArray());
        } else {
            SzLogger::get()->debug('completePaymentOrder:' . json_encode($result));
            $response->setContentType('text/html');
            $response->setBody($result);
        }
        return $response;
    }

    /**
     * 判断用户是否申请过权限，如果未申请，则加载注册页面
     * @return SzResponse | null
     */
    private function needRegisterFacebook()
    {
        // 当前玩家数据
        $appConfig = SzConfig::get()->loadAppConfig('app');
        $redirectUrl = $appConfig['CANVAS_URL'] . '/?' . http_build_query($_GET);
        $BG_IMG = SzUtility::getProtocol() . '//' . $appConfig['CDN_HOST'] . '/H5Client/bg.jpg';

        $smarty = SzSmarty::get();
        $smarty->assign('REDIRECT_URL', $redirectUrl);
        $smarty->assign('CANVAS_NAME', $appConfig['CANVAS_NAME']);
        $smarty->assign('BG_IMG', $BG_IMG);
        $smarty->assign('PLATFORM_JS_HTML', $this->createJsEmbeddedCode(true));

        $response = $this->buildResponse($smarty->fetch('register.tpl'));
        $response->setContentType('text/html');
        return $response;
    }

    /**
     * Display facebook payment product info.
     *
     * @return SzResponse
     */
    private function displayPaymentProductInfo()
    {
        $userId = SzParam::getReqParam('user_id');
        $paymentDefId = SzParam::getReqParam('id');
        $reqSerial = SzParam::getReqParam('serial');
        $response = new SzResponse();
        $response->setContentType('text/html');
        $response->setBody(PayService::get()->getPaymentDefInfo($userId, $paymentDefId, $reqSerial));
        return $response;
    }

    public function createJsEmbeddedCode($register = false)
    {
        $appId = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFFB), 'appId', true);
        $registerScope = '';
        if ($register) {
            $registerScope = 'registerScope()';
        }
        return "<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '" . $appId['value'] . "',
      xfbml      : true,
      oauth      : true,
      version    : 'v2.8'
    });
    " . $registerScope . "
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = \"//connect.facebook.net/en_US/sdk.js\";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>";
    }
}